//
//  TMUserCompanitController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/29.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMUserCompanitController.h"
#import "TMUserViewModel.h"
@interface TMUserCompanitController ()<UITextViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextView *complaintContentTextV;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextf;
@property (weak, nonatomic) IBOutlet UILabel *placeHolderLabel;
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableDictionary *param;
@end

@implementation TMUserCompanitController
-(NSMutableDictionary *)param{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isBussiness ? @"商家投诉" : @"平台投诉";
    self.viewModel = [[TMUserViewModel alloc]init];
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.complaintContentTextV.delegate = self;
    self.phoneTextf.delegate = self;
    self.complaintContentTextV.layer.borderWidth = 1.0f;
    self.complaintContentTextV.layer.borderColor = [UIColor hx_colorWithHexRGBAString:@"#666666"].CGColor;
    self.complaintContentTextV.layer.cornerRadius = 3.0f;
    self.complaintContentTextV.layer.masksToBounds = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didInsert:) name:UITextViewTextDidChangeNotification object:nil];
    
}

- (IBAction)sendContentAction:(UIButton *)sender {
    if (self.phoneTextf.text.length == 0 || !self.phoneTextf.text) {
        [TMProgressHUD showInfoWithStyleDarkStatus:@"电话号码不能为空！"];
        return;
    }
    if (self.phoneTextf.text.length != 11 ) {
        [TMProgressHUD showInfoWithStyleDarkStatus:@"请输入正确的电话号码！"];
        return;
    }
    if (self.complaintContentTextV.text.length == 0 || !self.complaintContentTextV.text) {
        [TMProgressHUD showInfoWithStyleDarkStatus:@"投诉内容不能为空！"];
        return;
    }
    if (self.isBussiness == NO) {
        [self.viewModel getComplaintWith:self.phoneTextf.text AndText:self.complaintContentTextV.text Success:^(id responseObject) {
            [self.navigationController popViewControllerAnimated:YES];
        } AndFiled:^(NSString *error) {
            
        }];
    }else{
        NSDictionary *dic = @{@"uid":@([TMUserManager deaufatManager].userModel.uid) ,@"token":[TMUserManager deaufatManager].userModel.token,@"phone":self.phoneTextf.text ,@"content":self.complaintContentTextV.text,@"id":@(self.models.oid),@"sid":@(self.models.sid)};
        [self.viewModel getComplaintBussinessWith:dic Success:^(id responseObject) {
            [self.navigationController popViewControllerAnimated:YES];
        } AndFiled:^(NSString *error) {
            
        }];
    }
}
- (void)didInsert:(UITextView *)text{
    if (self.complaintContentTextV.text.length > 0) {
        self.placeHolderLabel.hidden = YES;
    }else{
        self.placeHolderLabel.hidden = NO;
    }
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}
#pragma mark - Table view data source
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
